package cn.itcast.cron;

import cn.itcast.mapper.QuotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@EnableScheduling  //开启定时任务
@Component  //交给spring容器进行管理
public class CronTask {
    /**
     * 1.查询交易日历表获取交易日
     * 2.当前周K数据查询
     * 3.更新周K日期
     * 4.当前月K数据查询
     * 5.更新月K日期
     * 更新规则：
     * 有数据：将日期全部更新为最新日期
     * 无数据：不更新日期
     */

    @Autowired
    QuotMapper quotMapper;

    /**
     * 周/月 K线数据日期更新
     */
    @Scheduled(cron = "${cron.pattern.loader}")  //  0/10 * * * * ?
    public void update() {
        //todo 1.查询交易日历表获取交易日
        Map<String, Object> map = quotMapper.queryTccDate();
        //获取trade_date
        String tradeDate = map.get("trade_date").toString();

        //获取week_first_txdate
        String weekFirstTxDate = map.get("week_first_txdate").toString();

        //获取month_first_txdate
        String monthFirstTxdate = map.get("month_first_txdate").toString();

        //todo 2.当前周K数据查询
        List<Map<String,Object>> weekList = quotMapper.queryKline("bdp_quot_stock_kline_week",weekFirstTxDate,tradeDate);
        if(weekList!=null && weekList.size()>0){ //周K一定有数据
            //todo 3.更新周K日期
            quotMapper.updateKline("bdp_quot_stock_kline_week",weekFirstTxDate,tradeDate);
        }
        //todo 4.当前月K数据查询
        List<Map<String, Object>> monthList = quotMapper.queryKline("bdp_quot_stock_kline_month", monthFirstTxdate, tradeDate);
        //todo 5.更新月K日期
        if(monthList!=null && monthList.size()>0){
            quotMapper.updateKline("bdp_quot_stock_kline_month",monthFirstTxdate,tradeDate);
        }
    }
}
